package com.mapbar.pay.interceptor;

import com.mapbar.pay.entity.User;
import com.mapbar.pay.service.UserService;
import com.mapbar.pay.util.CacheUtil;
import com.mapbar.pay.util.ProjectUtil;
import com.mapbar.pay.util.Util;
import com.mapbar.util.IDCodec;
import com.mapbar.util.TextCodec;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 * @date  2010-3-4  15:08:57
 * @author fengbj
 */
public class AuthorityInterceptor extends AbstractInterceptor {

    public Logger logger = Logger.getLogger(this.getClass());//日志对象
    private UserService userService;

    @Override
    public String intercept(ActionInvocation ai) throws Exception {

        //HttpServletRequest request= (HttpServletRequest) ai.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
        //Cookie  cookies [] = request.getCookies();
        //ai.invoke();

        User user = Util.getUserInfoFromCookie(ServletActionContext.getRequest().getCookies());

        //通过从cookie里面获取的信息验证用户，如果userIdc解码后等于userId说明已经登录
        if (user != null && user.getUserId() != 0 && user.getUserIdc() != null) {
            //合法用户
            try {
                if ((user.getUserId()+"").equals(IDCodec.decode(TextCodec.decode(user.getUserIdc())))) {

                    //判断缓存中的用户信息是否已经过期
                    User userTemp = user;//临时user
                    logger.info("开始判断缓存中的用户信息是否已经过期 user = [" + user.toString() + "]");
                    Object temp = null;
                    try {
                        temp = CacheUtil.get(ProjectUtil.USER_PREFIX_IN_CACHE + user.getUserId());
                        if (temp != null) {
                            user = (User) temp;
                        } else {
                            user = null;
                        }
                    } catch (Exception e) {
                        user = null;
                        logger.error("判断缓存中用户信息是否已经过期是转型异常：" + e.toString());
                    } finally {
                        temp = null;
                    }

                    if (user == null) {
                        //用户信息已经过期
                        logger.info("缓存中用户信息过期：");
                        //更新用户信息
                        logger.info("刷新用户信息：");
                        userService.refreshUserInfo(userTemp);
                        ai.getInvocationContext().getParameters().put("user", userTemp);

                    } else {
                        //用户信息没有过期
                        ai.getInvocationContext().getParameters().put("user", user);
                    }

                }else{
                    logger.info("可疑用户访问，user=["+ user+"] ,userIdcEn=[" + IDCodec.decode(TextCodec.decode(user.getUserIdc()))+"]");
                }

            } catch (Exception e) {
                logger.error("登录拦截器异常：user["+user +"]"+e.toString());
            }

        } else {
            logger.info("未登录用户 [" + user + "]");
            ai.getInvocationContext().getParameters().put("user", null);
        }

        logger.info("进入action");
        String res = null;
        try {
             res = ai.invoke();
        } catch (Exception e) {
            logger.error(res + ","+e.toString());
            e.printStackTrace();
        }
        logger.info("actin res="+res);
        return res;

        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
